<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\Category;
use app\common\model\Yushou;
use \think\Request;
use think\facade\View;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsList;
use app\common\model\OrderList;

use app\common\model\AdminGoodsLog;
use think\facade\Db;
use app\common\model\Shang;

class Goods extends Base
{
    /**
     * 盒子列表
     */
    public function goods(Request $request)
    {
        $title = trim(input('get.title'));
        $status = trim(input('get.status'));
        $type = trim(input('get.type'));
        $whe = array();
        if ($title) {
            $whe[] = ['title', 'like', '%' . $title . '%'];
        }
        if ($status) {
            $whe[] = ['status', '=', $status];
        }
        if ($type) {
            $whe[] = ['type', '=', $type];
        } else {
            $whe[] = ['type', 'notIn', [4, 7, 11]];
        }
        $field = "*";
        $order = "id desc";
        $data = GoodsModel::getList($whe, $field, $order, $this->page);
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch("Goods/goods");
    }

    /**
     * 添加盒子
     */
    public function goods_add(Request $request)
    {
        if (!$request->isPost()) {
            $item_card = \app\common\model\ItemCard::where('id', '<', 3)->select()->toArray();
            $shang = Shang::where('id', 'between', [34, 38])->select()->toArray();

            View::assign('shang', $shang);
            View::assign('item_card', $item_card);
            return View::fetch("Goods/goods_add");
        } else {
            $data = input('post.');
            if (empty($data['title'])) {
                return $this->renderError("请输入盒子名称");
            }
            if (RegMoney($data['price'])) {
                return $this->renderError("盒子价格输入不规范,最多保留两位小数");
            }
            if (RegZero($data['sort'])) {
                return $this->renderError("排序输入不规范,请输入整数");
            }
            if ($data['type'] == 1) {
                if (RegInt($data['stock'])) {
                    return $this->renderError("库存输入不规范,请设置大于0的整数");
                }
                if ($data['lock_is'] == 1) {
                    if (RegInt($data['lock_time'])) {
                        return $this->renderError("请设置锁箱时间");
                    }
                } elseif ($data['lock_is'] == 0) {
                    $data['lock_time'] = 0;
                } else {
                    return $this->renderError("锁箱类型选择错误");
                }
                if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
                    return $this->renderError("发积分开关选择错误");
                }
                if ($data['show_is'] != 0 && $data['show_is'] != 1) {
                    return $this->renderError("首页显示开关选择错误");
                }
                $data['prize_num'] = 0;
                $data['category_id'] = 0;
            } elseif ($data['type'] == 2) {
                $data['stock'] = 0;
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['show_is'] = 0;
                $data['prize_num'] = 0;
                if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
                    return $this->renderError("发积分开关选择错误");
                }
                $data['category_id'] = 0;
            } elseif ($data['type'] == 3) {
                $data['stock'] = 1;
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['show_is'] = 0;
                $data['category_id'] = 0;
                $data['prize_num'] = 0;
                // if (RegInt($data['prize_num'])) {
                //     return $this->renderError("擂台赏抽全局赏数量设置错误,请设置大于0的整数");
                // }
                if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
                    return $this->renderError("发积分开关选择错误");
                }
            } elseif ($data['type'] == 5) {
                if (RegInt($data['stock'])) {
                    return $this->renderError("库存输入不规范,请设置大于0的整数");
                }
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['prize_num'] = 0;
                $data['show_is'] = 0;
                $data['coupon_is'] = 0;
                $data['coupon_pro'] = 0;
                $data['integral_is'] = 0;
                $data['category_id'] = 0;
            } elseif ($data['type'] == 6) {
                if (RegInt($data['stock'])) {
                    return $this->renderError("库存输入不规范,请设置大于0的整数");
                }
                if ($data['lock_is'] == 1) {
                    if (RegInt($data['lock_time'])) {
                        return $this->renderError("请设置锁箱时间");
                    }
                } elseif ($data['lock_is'] == 0) {
                    $data['lock_time'] = 0;
                } else {
                    return $this->renderError("锁箱类型选择错误");
                }
                if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
                    return $this->renderError("发积分开关选择错误");
                }
                if ($data['show_is'] != 0 && $data['show_is'] != 1) {
                    return $this->renderError("首页显示开关选择错误");
                }
                $data['prize_num'] = 0;
                $data['category_id'] = 0;
            } elseif ($data['type'] == 8) {
                $data['stock'] = 0;
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['show_is'] = 0;
                $data['prize_num'] = 0;
                if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
                    return $this->renderError("发积分开关选择错误");
                }
                $data['category_id'] = 0;
            } elseif ($data['type'] == 9) {
                $data['stock'] = 0;
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['show_is'] = 0;
                $data['prize_num'] = 0;
                if ($data['integral_is'] != 0 && $data['integral_is'] != 1) {
                    return $this->renderError("发积分开关选择错误");
                }
                $data['category_id'] = 0;
            } else {
                return $this->renderError("盒子类型选择错误");
            }
            $data['sale_time'] = 0;
            // if (isset($data['sale_time'])) {
            //     $data['sale_time'] = strtotime($data['sale_time']);
            // } else {
            //     $data['sale_time'] = 0;
            // }
            if (empty($data['imgurl'])) {
                return $this->renderError("请上传盒子封面图");
            }
            if (empty($data['imgurl_detail'])) {
                return $this->renderError("请上传盒子详情图");
            }
            $data['status'] = 2;
            $data['addtime'] = time();
            $dd = GoodsModel::insert($data);
            if ($dd) {
                return $this->renderSuccess("添加成功");
            } else {
                return $this->renderError("网络繁忙，请稍后");
            }
        }
    }

    /**
     * 编辑盒子
     */
    public function goods_edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = $request->param('id/d', 0);
            $info = GoodsModel::where(['id' => $id])->find();
            if (!$info) {
                return $this->renderError("请求参数错误");
            }
            $info['sale_time'] = '';
            // if ($info['sale_time']) {
            //     $info['sale_time'] = date('Y-m-d', $info['sale_time']);
            // } else {
            //     $info['sale_time'] = '';
            // }
            $type = $info['type'];
            $item_card = \app\common\model\ItemCard::where('id', '<', 3)->select()->toArray();
            $shang = Shang::where('id', 'between', [34, 38])->select()->toArray();
            View::assign('shang', $shang);
            View::assign('item_card', $item_card);
            View::assign('type', $type);
            View::assign('info', $info);
            return View::fetch("Goods/goods_edit");
        } else {
            $data = input('post.');
            if (empty($data['id'])) {
                return $this->renderError("请求参数错误");
            }
            $info = GoodsModel::where(['id' => $data['id']])->find();
            if (!$info) {
                return $this->renderError("请求参数错误1");
            }
            if (empty($data['title'])) {
                return $this->renderError("请输入盒子名称");
            }
            if (RegMoney($data['price'])) {
                return $this->renderError("盒子价格输入不规范,最多保留两位小数");
            }
            if (RegZero($data['sort'])) {
                return $this->renderError("排序输入不规范,请输入整数");
            }
            $type = $info['type'];
            if ($type == 1) {
                if (RegInt($data['stock'])) {
                    return $this->renderError("库存输入不规范,请设置大于0的整数");
                }
                if ($data['lock_is'] == 1) {
                    if (RegInt($data['lock_time'])) {
                        return $this->renderError("请设置锁箱时间");
                    }
                } elseif ($data['lock_is'] == 0) {
                    $data['lock_time'] = 0;
                } else {
                    return $this->renderError("锁箱类型选择错误");
                }
                $data['prize_num'] = 0;
                $data['category_id'] = 0;
            } elseif ($type == 2) {
                $data['stock'] = 0;
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['prize_num'] = 0;
                $data['category_id'] = 0;
            } elseif ($type == 3) {
                if ($info['stock'] != $data['stock']) {
                    return $this->renderError("不可修改库存");
                }
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['category_id'] = 0;
                $data['prize_num'] = 0;
            } elseif ($type == 5) {
                if (RegInt($data['stock'])) {
                    return $this->renderError("库存输入不规范,请设置大于0的整数");
                }
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['prize_num'] = 0;
                $data['coupon_is'] = 0;
                $data['coupon_pro'] = 0;
                $data['integral_is'] = 0;
                $data['category_id'] = 0;
            } elseif ($type == 6) {

                if (RegInt($data['stock'])) {
                    return $this->renderError("库存输入不规范,请设置大于0的整数");
                }
                if ($data['lock_is'] == 1) {
                    if (RegInt($data['lock_time'])) {
                        return $this->renderError("请设置锁箱时间");
                    }
                } elseif ($data['lock_is'] == 0) {
                    $data['lock_time'] = 0;
                } else {
                    return $this->renderError("锁箱类型选择错误");
                }
                if ($data['show_is'] != 0 && $data['show_is'] != 1) {
                    return $this->renderError("首页显示开关选择错误");
                }
                $data['prize_num'] = 0;
                $data['category_id'] = 0;
            } elseif ($type == 8) {
                $data['stock'] = 0;
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['prize_num'] = 0;
                $data['category_id'] = 0;
            } elseif ($type == 9) {
                $data['stock'] = 0;
                $data['lock_is'] = 0;
                $data['lock_time'] = 0;
                $data['prize_num'] = 0;
                $data['category_id'] = 0;
            } else {
                return $this->renderError("盒子类型选择错误");
            }
            $data['sale_time'] = 0;
            // if (isset($data['sale_time'])) {
            //     $data['sale_time'] = strtotime($data['sale_time']);
            // } else {
            //     $data['sale_time'] = 0;
            // }
            if (empty($data['imgurl'])) {
                return $this->renderError("请上传盒子封面图");
            }
            if (empty($data['imgurl_detail'])) {
                return $this->renderError("请上传盒子封面图");
            }
            $data['update_time'] = time();
            $order_list_id = OrderList::where(['goods_id' => $data['id']])->value('id');
            if (!empty($order_list_id)) {
                if ($data['stock'] < $info['stock']) {
                    return $this->renderError("不能减少库存");
                }
            }

            Db::startTrans();
            $res = [];
            #添加套
            if ($type == 1 || $type == 5 || $type == 6) {
                if (($data['stock'] > $info['stock'])) {
                    #赏品
                    $goods_list = GoodsList::where(['goods_id' => $info['id']])
                        ->where(['num' => 1])
                        ->select()->toArray();
                    if ($goods_list) {
                        #循环数据
                        $save_sports_data = [];
                        $start_num = $info['stock'] + 1;
                        for ($i = $start_num; $i <= $data['stock']; $i++) {
                            foreach ($goods_list as $k => $v) {
                                unset($v['id']);
                                unset($v['num']);
                                $v['num'] = $i;
                                $v['surplus_stock'] = $v['stock'];
                                $save_sports_data[] = $v;
                            }
                        }
                        #添加赏品
                        $res[] = GoodsList::insertAll($save_sports_data);
                    }
                }
            }
            $res[] = $info->allowField([])->update($data);
            //添加日志
            $info['update_time'] = date('Y-m-d H:i:i:s', $info['update_time']);
            $data['update_time'] = date('Y-m-d H:i:i:s', $data['update_time']);
            $res[] = AdminGoodsLog::add_goods_log(session('admin_id'), $info['id'], 0, json_encode($info), json_encode($data));
            if (resCheck($res)) {
                Db::commit();
                return $this->renderSuccess("编辑成功");
            } else {
                Db::rollback();
                return $this->renderError("网络繁忙，请稍后");
            }
        }
    }

    /**
     * 上、下架,删除盒子
     */
    public function goods_del(Request $request)
    {
        $id = $request->post('id/d');
        $status = $request->post('type/d');
        $data = GoodsModel::where(['id' => $id])->field('id')->find();
        if (!$data) {
            return $this->renderError("数据不存在");
        }
        if ($status == 1 || $status == 2) {
            $result = GoodsModel::where(['id' => $id])->update(['status' => $status]);
        } elseif ($status == 3) {
            $result = GoodsModel::where(['id' => $id])->update(['delete_time' => time(), 'status' => 2]);
            //            $goods = GoodsList::field('id')->where(['goods_id' => $id])->find();
            //            if ($goods) {
            //                GoodsList::field('id')->where(['goods_id' => $id])->delete();
            //            }
        } elseif ($status == 4) {
            // return $this->renderError("网络繁忙，请稍后");
            $w = [];
            $w[] = ['id', '=', $id];
            $goods = GoodsModel::getInfo($w, '*');
            $goods = $goods->toArray();
            $ww = [];
            $ww[] = ['goods_id', '=', $id];
            $goodslist = GoodsList::getAllList($ww, '*');

            unset($goods['id']);
            $goods['status'] = 2;
            GoodsModel::startTrans();
            $dd = GoodsModel::insertGetId($goods);
            $goodslist = $goodslist->toArray();
            foreach ($goodslist as $k => &$v) {
                unset($v['id']);
                $v['goods_id'] = $dd;
                $v['stock'] = $v['stock'];
                if (in_array($goods['type'], [2, 8, 9])) {
                    $v['stock'] = $v['gailv'];
                }
                if (in_array($goods['type'], [1, 3, 5, 6])) {
                    $v['surplus_stock'] = $v['stock'];
                }
                $time = time() + $k;
                $w = [];
                $w[] = ['addtime', '=', $time];
                $res = GoodsList::getInfo($w, 'id,title');
                if (!empty($res)) {
                    GoodsModel::rollback();
                    return $this->renderError("复制过快，请稍后在复制");
                }
                $v['addtime'] = $time;
            }
            $ss = GoodsList::insertAll($goodslist);
            if ($dd && $ss) {
                GoodsModel::commit();
                return $this->renderSuccess("复制成功");
            } else {
                GoodsModel::rollback();
                return $this->renderError("复制失败");
            }
        } else {
            return $this->renderError("请求参数错误");
        }
        if ($result) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("网络繁忙，请稍后");
        }
    }

    /**
     * 盒子奖品
     */
    public function goodslist(Request $request)
    {
        $goods_id = $request->param('goods_id/d', 0);
        $info = GoodsModel::where(['id' => $goods_id])->find();
        if (!$info) {
            return $this->renderError('请求参数错误');
        }
        $type = $info['type'];
        $title = trim(input('get.title'));
        $lian_ji_type = input('get.lian_ji_type');
        $whe = array();
        $whe[] = ['goods_id', '=', $goods_id];

        if ($type == 1 || $type == 3 || $type == 5 || $type == 6) {
            $whe[] = ['num', '=', 1];
        } elseif ($type == 2 || $type == 8 || $type == 9) {
            $whe[] = ['num', '=', 0];
        } else {
            return $this->renderError('请求参数错误1');
        }
        if (!empty($title)) {
            $whe[] = ['title', 'like', '%' . $title . '%'];
        }
        if (!empty($lian_ji_type)) {
            $whe[] = ['lian_ji_type', '=', $lian_ji_type];
        }
        // SELECT * FROM `goods_list` WHERE `goods_id`=337 
        $field = "*";
        $order = "sort desc,shang_id asc,id asc";
        $data = GoodsList::getList($whe, $field, $order, 150);
        $nums = GoodsList::where($whe)->sum('gailv');
        $reveal_nums = GoodsList::where($whe)->sum('reveal_gailv');
        $real_pro = 0;
        $reveal_real_pro = 0;
        foreach ($data['list'] as &$value) {
            $value['shang'] = Shang::where(['id' => $value['shang_id']])->value('title');
            if ($nums > 0) {
                $value['num'] = self::jisuan(self::jisuan($value['gailv'], $nums, '/', 5), 100, '*', 3);
            } else {
                $value['num'] = 0;
            }
            $real_pro += $value['num'];
            if ($reveal_nums > 0) {
                $value['reveal_num'] = self::jisuan(self::jisuan($value['reveal_gailv'], $reveal_nums, '/', 5), 100, '*', 3);
            } else {
                $value['reveal_num'] = 0;
            }
            $reveal_real_pro += $value['reveal_num'];
        }
        unset($value);

        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        View::assign('goods_id', $goods_id);
        View::assign('type', $type);
        View::assign('real_pro', $real_pro);
        View::assign('reveal_real_pro', $reveal_real_pro);
        return View::fetch("Goods/goodslist");
    }

    public static function jisuan($num1, $num2, $opt, $length)
    {
        // dd($num1);
        switch ($opt) {
            case "+":
                $rs = bcadd("$num1", "$num2", $length);
                break;
            case "-":
                $rs = bcsub("$num1", "$num2", $length);
                break;
            case "*":
                $rs = bcmul("$num1", "$num2", $length);
                // dd($num1.'=='.$num2.'=='.$rs);
                break;
            case "/":
                if ($num2 == 0) {
                    exit("被除数不能为0！");
                } else {
                    $rs = bcdiv("$num1", "$num2", $length);
                }
                break;
            default:
                echo "异常操作！";
                break;
        }
        return $rs;
    }


    /**
     * 添加奖品
     */
    public function goodslist_add(Request $request)
    {
        if (!$request->isPost()) {
            $goods_id = $request->param('goods_id/d', 0);
            $info = GoodsModel::where(['id' => $goods_id])->find();
            if (!$info) {
                return $this->renderError('请求参数错误');
            }
            if ($info['type'] == 1 || $info['type'] == 5 || $info['type'] == 6) {
                $shang = Shang::where('id', '<=', 33)->where('id', '<>', 5)->select()->toArray();
            } elseif ($info['type'] == 2 || $info['type'] == 8 || $info['type'] == 9) {
                $shang = Shang::where('id', 'between', [34, 38])->select()->toArray();
            } elseif ($info['type'] == 3) {
                $shang = Shang::where('id', 'between', [4, 33])->select()->toArray();
            } else {
                return $this->renderError('请求参数错误1');
            }
            View::assign('shang', $shang);
            View::assign('goods_id', $goods_id);
            View::assign('type', $info['type']);
            return View::fetch('Goods/goodslist_add');
        } else {
            $data = input('post.');
            if (empty($data['goods_id'])) {
                return $this->renderError('请求参数错误1');
            }
            $info = GoodsModel::where(['id' => $data['goods_id']])->find();
            if (!$info) {
                return $this->renderError('请求参数错误2');
            }
            $type = $info['type'];
            if (empty($data['title'])) {
                return $this->renderError('请输入奖品名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (RegMoney($data['price'])) {
                return $this->renderError('奖品售价设置错误,最多保留两位小数');
            }
            if (RegMoney($data['money'])) {
                return $this->renderError('兑换价格设置错误,最多保留两位小数');
            }
            if (RegMoney($data['sc_money'])) {
                return $this->renderError('参考价格设置错误,最多保留两位小数');
            }
            if ($data['goods_type'] == 1) {
                $data['sale_time'] = 0;
            } else if (in_array($data['goods_type'], [2, 8, 9])) {
                if (empty($data['sale_time'])) {
                    return $this->renderError('请选择预售时间');
                }
                $today_time = strtotime(date('Y-m-d', time()));
                $data['sale_time'] = strtotime($data['sale_time']);
                if ($today_time >= $data['sale_time']) {
                    return $this->renderError('预售时间请在今天之后');
                }
            } else {
                return $this->err('奖品类型选择错误');
            }
            //2023-11-17无作用
            $data['reward_num'] = 0;
            //

            if ($type == 1 || $type == 5 || $type == 6) {
                if ($data['shang_id'] == 1 || $data['shang_id'] == 2 || $data['shang_id'] == 3 || $data['shang_id'] == 5 || $data['shang_id'] == 6) {
                    if ($data['stock'] != 1) {
                        return $this->renderError('特殊奖品库存请设置为1');
                    }
                }
                if (RegInt($data['stock'])) {
                    return $this->renderError('奖品数量设置错误,请设置大于0的整数');
                }
                $data['surplus_stock'] = $data['stock'];
                #盒子套数
                $stock = $info['stock'];
                #循环数据
                $save_sports_data = [];
                $prize_code = getPrizeCode() . '_' . time();
                for ($i = 1; $i <= $stock; $i++) {
                    $data['num'] = $i;
                    $data['prize_code'] = $prize_code;
                    $save_sports_data[] = $data;
                }
            } elseif (in_array($type, [2, 8, 9])) {
                // if (RegMoney($data['real_pro']*100)) {
                //     return $this->renderError('真实概率设置错误,最多保留两位小数1');
                // }

                $prize_code = getPrizeCode() . '_' . time();
                $data['prize_code'] = $prize_code;
            } else if ($type == 3) {
                if ($data['shang_id'] == 1 || $data['shang_id'] == 2 || $data['shang_id'] == 3 || $data['shang_id'] == 5 || $data['shang_id'] == 6) {
                    if ($data['stock'] != 1) {
                        return $this->renderError('特殊奖品库存请设置为1');
                    }
                }
                if (RegInt($data['stock'])) {
                    return $this->renderError('奖品数量设置错误,请设置大于0的整数');
                }
                $data['surplus_stock'] = $data['stock'];
                #盒子套数
                $stock = $info['stock'];
                #循环数据
                $save_sports_data = [];
                $prize_code = getPrizeCode() . '_' . time();
                for ($i = 1; $i <= $stock; $i++) {
                    $data['num'] = $i;
                    $data['prize_code'] = $prize_code;
                    $save_sports_data[] = $data;
                }
                if (RegInt($data['prize_num']) && $data['shang_id'] == 5) {
                    return $this->renderError("擂台赏抽全局赏数量设置错误,请设置大于0的整数");
                }
            } else {
                return $this->err('请求参数错误');
            }
            if (RegZero($data['sort'])) {
                return $this->renderError('排序请输入整数');
            }
            //            if ($data['card_no']) {
            //                $card_no_info = GoodsList::field('id')->where('card_no', '=', $data['card_no'])->find();
            //                if ($card_no_info) {
            //                    return $this->renderError('赠送编号已存在');
            //                }
            //            } else {
            $data['card_no'] = NULL;
            //            }

            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $data['addtime'] = time();
            if (isset($save_sports_data)) {
                $res = GoodsList::insertAll($save_sports_data);
            } else {
                $res = GoodsList::insert($data);
            }
            if ($res) {
                return $this->renderSuccess('添加成功');
            } else {
                return $this->renderError('添加失败');
            }
        }
    }

    /**
     * 编辑奖品
     */
    public function goodslist_edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = $request->param('id/d', 0);
            $goods = GoodsList::where(['id' => $id])->find();
            if (!$goods) {
                return $this->renderError('请求参数错误');
            }
            if ($goods['goods_type'] == 2) {
                $goods['sale_time'] = date('Y-m-d', $goods['sale_time']);
            } else {
                $goods['sale_time'] = '';
            }
            $goods_id = $goods['goods_id'];
            $info = GoodsModel::where(['id' => $goods_id])->find();
            if (!$info) {
                return $this->renderError('请求参数错误1');
            }
            if ($info['type'] == 1 || $info['type'] == 5 || $info['type'] == 6) {
                $shang = Shang::where('id', '<=', 33)->where('id', '<>', 5)->select()->toArray();
            } elseif ($info['type'] == 2 || $info['type'] == 8 || $info['type'] == 9) {
                $shang = Shang::where('id', 'between', [34, 38])->select()->toArray();
            } elseif ($info['type'] == 3) {
                $shang = Shang::where('id', 'between', [4, 33])->select()->toArray();
            } else {
                return $this->renderError('请求参数错误1');
            }

            View::assign('goods', $goods);
            View::assign('shang', $shang);
            View::assign('type', $info['type']);
            return View::fetch('Goods/goodslist_edit');
        } else {

            $data = input('post.');
            if (empty($data['id'])) {
                return $this->renderError('请求参数错误1');
            }
            $goods = GoodsList::where(['id' => $data['id']])->find();
            if (!$goods) {
                return $this->renderError('请求参数错误2');
            }
            $info = GoodsModel::where(['id' => $goods['goods_id']])->find();
            if (!$info) {
                return $this->renderError('请求参数错误3');
            }
            $type = $info['type'];
            if (empty($data['title'])) {
                return $this->renderError('请输入奖品名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (RegMoney($data['price'])) {
                return $this->renderError('奖品售价设置错误,最多保留两位小数');
            }
            if (RegMoney($data['money'])) {
                return $this->renderError('兑换价格设置错误,最多保留两位小数');
            }
            if (RegMoney($data['sc_money'])) {
                return $this->renderError('参考价格设置错误,最多保留两位小数');
            }
            if ($data['goods_type'] == 1) {
                $data['sale_time'] = 0;
            } else if ($data['goods_type'] == 2) {
                if (empty($data['sale_time'])) {
                    return $this->renderError('请选择预售时间');
                }
                $today_time = strtotime(date('Y - m - d', time()));
                $data['sale_time'] = strtotime($data['sale_time']);
                if ($today_time >= $data['sale_time']) {
                    return $this->renderError('预售时间请在今天之后');
                }
            } else {
                return $this->err('奖品类型选择错误');
            }

            //2023-11-17无作用
            $data['reward_num'] = 0;
            //

            if ($type == 1 || $type == 5 || $type == 6) { } elseif ($type == 2 || $type == 8 || $type == 9) {
                // if (RegMoney($data['real_pro']*100)) {
                // return $this->renderError('真实概率设置错误,最多保留两位小数');
                // }
            } else if ($type == 3) {
                if (RegInt($data['prize_num']) && $data['shang_id'] == 5) {
                    return $this->renderError("擂台赏抽全局赏数量设置错误,请设置大于0的整数");
                }
            } else {
                return $this->err('请求参数错误');
            }
            if (RegZero($data['sort'])) {
                return $this->renderError('排序请输入整数');
            }
            //            if ($data['card_no']) {
            //                $card_no_info = GoodsList::field('id')
            //                    ->where('prize_code', '<>', $goods['prize_code'])
            //                    ->where('card_no', '=', trim($data['card_no']))
            //                    ->find();
            //                if ($card_no_info) {
            //                    return $this->renderError('赠送编号已存在');
            //                }
            //            } else {
            $data['card_no'] = NULL;
            //            }

            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $data['update_time'] = time();
            unset($data['id']);
            $res1 = GoodsList::where(['prize_code' => $goods['prize_code']])->update($data);

            if ($res1) {
                //添加日志
                $goods['update_time'] = date('Y-m-d H:i:i:s', $goods['update_time']);
                $data['update_time'] = date('Y-m-d H:i:i:s', $data['update_time']);
                $res2 = AdminGoodsLog::add_goods_log(session('admin_id'), $info['id'], $goods['id'], json_encode($goods), json_encode($data));
                return $this->renderSuccess('编辑成功');
            } else {
                return $this->renderError('编辑失败');
            }
        }
    }


    /**
     * 奖品删除
     */
    public function goodslist_del(Request $request)
    {
        $id = \request()->param('id/d', 0);
        $prize_code = GoodsList::field('prize_code')
            ->where(['id' => $id])
            ->value('prize_code');
        if (!$prize_code) {
            return $this->renderError("请勿重复操作");
        }
        #删除奖品
        $res = GoodsList::field('id')
            ->where(['prize_code' => $prize_code])
            ->delete();
        if ($res) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("操作失败");
        }
    }


    /**
     * 消费赠送
     */
    public function give()
    {
        $title = trim(input('get.title'));
        $whe = array();
        $whe[] = ['goods_id', '=', 0];
        if (!empty($title)) {
            $whe[] = ['title', 'like', '%' . $title . '%'];
        }
        $field = "*";
        $order = "give_money asc,id asc";
        $data = GoodsList::getList($whe, $field, $order, $this->page);
        foreach ($data['list'] as &$value) {
            $value['shang'] = Shang::where(['id' => $value['shang_id']])->value('title');
        }

        $range_time = getConfig('give_time')['range_time'];
        View::assign('range_time', $range_time);
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch("Goods/give");
    }

    /**
     * 消费赠送添加
     */
    public function give_add(Request $request)
    {
        if (!$request->isPost()) {
            $shang = Shang::where('id', 'between', [10, 33])->select()->toArray();
            View::assign('shang', $shang);
            return View::fetch('Goods/give_add');
        } else {
            $data = input('post.');
            if (empty($data['title'])) {
                return $this->renderError('请输入奖品名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (RegMoney($data['price'])) {
                return $this->renderError('奖品售价设置错误,最多保留两位小数');
            }
            if (RegMoney($data['money'])) {
                return $this->renderError('兑换价格设置错误,最多保留两位小数');
            }
            if (RegInt($data['give_money'])) {
                return $this->renderError('消费阀值设置错误,请设置大于0的整数');
            }
            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $prize_code = getPrizeCode() . '_' . time();
            $data['prize_code'] = $prize_code;
            $data['goods_id'] = 0;
            $data['addtime'] = time();
            $dd = (new GoodsList())->insert($data);
            if ($dd) {
                return $this->renderSuccess('添加成功');
            } else {
                return $this->renderError('添加失败');
            }
        }
    }

    /**
     * 消费赠送编辑
     */
    public function give_edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = request()->param('id/d', 0);
            $info = GoodsList::where(['id' => $id])->find();
            if (!$info) {
                return $this->renderError('请求参数错误');
            }
            $shang = Shang::where('id', 'between', [10, 33])->select()->toArray();
            View::assign('shang', $shang);
            View::assign('info', $info);
            return View::fetch('Goods/give_edit');
        } else {
            $data = input('post.');
            if (empty($data['id'])) {
                return $this->renderError('请求参数错误');
            }
            $info = GoodsList::where(['id' => $data['id']])->find();
            if (!$info) {
                return $this->renderError('请求参数错误1');
            }
            if (empty($data['title'])) {
                return $this->renderError('请输入奖品名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (RegMoney($data['price'])) {
                return $this->renderError('奖品售价设置错误,最多保留两位小数');
            }
            if (RegMoney($data['money'])) {
                return $this->renderError('兑换价格设置错误,最多保留两位小数');
            }
            if (RegInt($data['give_money'])) {
                return $this->renderError('消费阀值设置错误,请设置大于0的整数');
            }
            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $data['update_time'] = time();
            unset($data['id']);
            $dd = $info->allowField([])->save($data);
            if ($dd) {
                return $this->renderSuccess('编辑成功');
            } else {
                return $this->renderError('编辑失败');
            }
        }
    }

    /**
     * 消费赠送删除
     */
    public function give_del(Request $request)
    {
        $id = \request()->param('id/d', 0);
        $info = GoodsList::field('prize_code')
            ->where(['id' => $id])
            ->find();
        if (!$info) {
            return $this->renderError("请勿重复操作");
        }
        #删除奖品
        $res = GoodsList::field('id')
            ->where(['id' => $id])
            ->delete();
        if ($res) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("操作失败");
        }
    }

    #无限令规则
    public function give_time()
    {
        $data = request()->param();
        $data['time'] = time();
        $result = setConfig('give_time', $data);
        if ($result) {
            return $this->renderSuccess('修改成功');
        } else {
            return $this->renderError('修改失败');
        }
    }


    /**
     * 预售日历
     */
    public function yushou_rili()
    {
        $title = trim(input('get.title'));
        $whe = array();
        if ($title) {
            $whe[] = ['title', 'like', '%' . $title . '%'];
        }
        $field = "*";
        $order = "sort desc,id desc";
        $data = Yushou::getList($whe, $field, $order, $this->page);
        foreach ($data['list'] as &$value) {
            $value['goods_title'] = GoodsModel::where(['id' => $value['goods_id']])->value('title');
            $value['sale_time'] = date('Y-m-d', $value['sale_time']);
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch("Goods/yushou_rili");
    }

    /**
     * 预售日历添加
     */
    public function yushou_rili_add(Request $request)
    {
        if (!$request->isPost()) {
            $goods = GoodsModel::where('type', '=', 1)->select()->toArray();
            View::assign('goods', $goods);
            return View::fetch('Goods/yushou_rili_add');
        } else {
            $data = input('post.');
            if (empty($data['title'])) {
                return $this->renderError('请输入标题');
            }
            if ($data['goods_id']) {
                $goods = GoodsModel::where('type', '=', 1)->where('id', '=', $data['goods_id']);
                if (!$goods) {
                    return $this->renderError('盒子选择错误');
                }
            } else {
                $data['goods_id'] = 0;
            }
            if (empty($data['sale_time'])) {
                return $this->renderError('请选择预售时间');
            }
            $data['sale_time'] = strtotime($data['sale_time']);
            $today_time = strtotime(date('Y-m-d', time()));
            if ($data['sale_time'] < $today_time) {
                return $this->renderError('预售时间选择错误');
            }
            if (RegZero($data['sort'])) {
                return $this->renderError('排序设置错误,请输入整数');
            }
            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $data['addtime'] = time();
            $dd = (new Yushou())->insert($data);
            if ($dd) {
                return $this->renderSuccess('添加成功');
            } else {
                return $this->renderError('添加失败');
            }
        }
    }

    /**
     * 预售日历编辑
     */
    public function yushou_rili_edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = request()->param('id/d', 0);
            $info = Yushou::where(['id' => $id])->find();
            if (!$info) {
                return $this->renderError('请求参数错误');
            }
            $info['sale_time'] = date('Y-m-d', $info['sale_time']);
            $goods = GoodsModel::where('type', '=', 1)->select()->toArray();
            View::assign('goods', $goods);
            View::assign('info', $info);
            return View::fetch('Goods/yushou_rili_edit');
        } else {
            $data = input('post.');
            if (empty($data['id'])) {
                return $this->renderError('请求参数错误');
            }
            $info = Yushou::where(['id' => $data['id']])->find();
            if (!$info) {
                return $this->renderError('请求参数错误1');
            }
            if (empty($data['title'])) {
                return $this->renderError('请输入标题');
            }
            if ($data['goods_id']) {
                $goods = GoodsModel::where('type', '=', 1)->where('id', '=', $data['goods_id']);
                if (!$goods) {
                    return $this->renderError('盒子选择错误');
                }
            } else {
                $data['goods_id'] = 0;
            }
            if (empty($data['sale_time'])) {
                return $this->renderError('请选择预售时间');
            }
            $data['sale_time'] = strtotime($data['sale_time']);
            $today_time = strtotime(date('Y-m-d', time()));
            if ($data['sale_time'] < $today_time) {
                return $this->renderError('预售时间选择错误');
            }
            if (RegZero($data['sort'])) {
                return $this->renderError('排序设置错误,请输入整数');
            }
            if (empty($data['imgurl'])) {
                return $this->renderError('请上传图片');
            }
            $data['update_time'] = time();
            unset($data['id']);
            $dd = $info->allowField([])->save($data);
            if ($dd) {
                return $this->renderSuccess('编辑成功');
            } else {
                return $this->renderError('编辑失败');
            }
        }
    }

    /**
     * 预售日历删除
     */
    public function yushou_rili_del(Request $request)
    {
        $id = \request()->param('id/d', 0);
        $info = Yushou::field('id')->where(['id' => $id])->find();
        if (!$info) {
            return $this->renderError("请勿重复操作");
        }
        #删除奖品
        $res = Yushou::field('id')->where(['id' => $id])->delete();
        if ($res) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("操作失败");
        }
    }


    /**
     * 增加概率
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function gailv(Request $request)
    {
        $id = $request->post('id/d');
        $num = $request->post('num/d');
        if ($num < 0) {
            return $this->renderError("概率不能为负数");
        }
        // if ($num == 0) {
        //     return $this->renderError("概率不能为零");
        // }
        $dd = GoodsList::where(['id' => $id])->update(["gailv" => $num, 'stock' => $num]);
        if ($dd) {
            return $this->renderSuccess("修改成功");
        } else {
            return $this->renderError("网络繁忙，请稍后");
        }
    }
    /**
     * 增加概率
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function reveal_gailv(Request $request)
    {
        $id = $request->post('id/d');
        $num = $request->post('num/d');
        if ($num < 0) {
            return $this->renderError("概率不能为负数");
        }
        // if ($num == 0) {
        //     return $this->renderError("概率不能为零");
        // }
        $dd = GoodsList::where(['id' => $id])->update(["reveal_gailv" => $num, 'reveal_stock' => $num]);
        if ($dd) {
            return $this->renderSuccess("修改成功");
        } else {
            return $this->renderError("网络繁忙，请稍后");
        }
    }
}
